The Programming Examples

In the directory \lcc\examples[1], you will find many examples of Windows programming, adapted to lcc-win32 from the Microsoft Software Development Kit, and from other sources.[2]

 

Name

Purpose

Aclapi            

Security API functions.

Agrep

Approximative grep. Finds similar words.

Bignums

The complete source code for the bignums multiple precision library

bzip2

Compression library. It compresses better than zip in most cases.

callexcel

Calls excel using COM dispatch interface

Cpuid

Displays information about the type of CPU that is running in your machine.

Console

Console (text mode) functions for managing the cursor, reading from the screen buffer, setting the video modes, etc.

Dll                   

Simple DLL.

Errdecode

Utility to show a clear text error description from the HRESULT or WIN32 error codes. Located in \lcc\src\utilities\errdecode.

Exetype

Utility showing the type of executable of the given file: Windows or console.

Floppy

How to format a floppy under program control

GetDxversion

Returns the version number of directx

Gdidemo

GDI window functions.

Guidgen

Utility to generate a Globally Unique Identifier (GUID). Located in \lcc\src\utilities\guidgen.

Iconpro

Icon management.

Intrinsic

The intrinsics functions of lcc-win32.

Halftone

The Halftone sample allows you to load and display bitmaps while allowing you to dynamically changing the color filters that are applied.

Hello              

Hello world under Windows.

Htmldlg

Calling a Java application that displays a dialog box from C.

Mandel

Mandelbrot drawing.

mc

Source of the message compiler

md5

Calculates the md5 checksum for a string or a file

Pedump

The complete source code of the Pedump utility.

Perfmon

Performance monitoring.

Regexp

Regular expression library

Reitp

How to use the Rich Text edit control

Roids             

Arcade game.

Scrnsave

Screen saver sample

Service

A sample service (Windows NT only).

Stdlib

Many examples illustrating different functions of the standard library of the C language.

Tasklist

Shows the list of running processes.

Uudecode

Utility to transform binary files into a transportable ASCII representation and back.

Wincapture

Mouse capture.

 

Winbase: basic windows services

fcopy

Copies a file into another using the file mapping APIs.

monkey

Registry monitor and other registry APIs.

tty

Demonstrates the serial communications primitives of win32, and the handling of the serial port.

filer

This is a complete clone of the file manager in Windows 3.1, using all file handling primitives, user interface gadgets, etc. of Windows 32.

crypto

Demonstrates how to use the crypto API to save a password encrypted in the registry.

serialdll

Allows you to use the serial port with a high level API

Network

Network examples sub-directory

Network\ping

The famous ‘ping’  utility.

Network\wsock

Win-sockets example.

Network\overlap

Overlapped I/O and TCP.

Network\ftp

Ftp client with all basic functionality running.

Network\macaddr

Prints the Ethernet adapter hardware address

Directx

Direct-X examples sub-directory

Directx\quat

Quaternions.

Directx\misc

Utility functions.

Directx\lib

Utility lib.

Directx\faces

Graphic example.

Directx\egg

Rotating egg in 3D.

Directx\mosquito

Another 3D example.

Directx\tunnel

Ditto.

Winui

Win user interface sub-directory

Winui\menu

Menu handling.

winui\mdi

Multiple Document Interface (MDI) application. Noteworthy is that there is a sample of a configuration key using the security primitives. Different configuration settings are stored according to whether the user is an administrator or not.

Winui\fileopen

Opening a file. It shows the use of the file open common dialog box and a hook procedure for GetOpenFileName.

Winui\rtfedit

RTF editor.

Winui\commondialog

Common dialog box examples. This is an interactive program that asks you for the parameters to pass to each common dialog function, showing you the modifications the parameters make.

Winui\clipboard

Clipboard management.

Winui \ rebar

New rebar control.

Winui \ switcher

Provides several active desktops.

Winui\appbar

Provides an application bar like the Windows desktop bar.

Fiber

Fiber management (Windows NT only).

 

All the examples come with a corresponding Makefile, so building them is just a matter of changing to the specific directory, and then typing

‘make

at the command line prompt.

Most makefiles come with an option , ‘clean’, which will delete all the objects and executables.

Some of them, particularly those that use the Direct X interface, may not run in all machines because either Direct X is not installed, or the screen driver does not support a specific direct x call.

The examples provide a library of common code that you can use in your applications, sparing you hours of work. I use them still, after many years, just by cutting/pasting from that code into a new application. This is a simple, but effective way of code re-use.

The Standard Library Examples

For those users who are beginning with the C language, start with the examples in the ‘stdlib’ directory . They are very simple code snippets, but provide you with an idea of how the language works, and with some useful programs, for instance, the utility ‘paste’, which mimics the same utility of UNIX, or a utility to convert C source into html source. They are:

 

abs.c

Absolute value.

asctime.c

Printing a time value.

asin.c

Asin function (trig).

assert.c

Assert macro.

atexit.c

Defining a function to be executed at program’s exit.

atof.c

Converting a character string into double.

c2html.c

Convert C into html. Contributed by David Hoffman.

ceil.c

Ceil function.

clock.c

Measuring elapsed time with clock().

copysign.c

Copysign: extending the sign of a double.

cos.c

Trig.

cpuid.c

Using the cpuid intrinsic.

ctime.c

Printing a time value.

difftime.c

Finding out the difference between two time values.

fabs.c

Float absolute value.

fflush.c

Flushing a file.

fgetc.c

Get a char from a file.

fpreset.c

Floating point reset function.

hypot.c

Trig.

ldexp.c

The exponent a mantissa values of a number.

log.c

Logarithm.

modf.c

Floating point modulus.

paste.c

A ‘paste ‘ utility.

pow.c

Floating point power function.

remove.c

Erasing a file.

rint.c

Round to integer.

sqrt.c

Square root.

strdate.c

Date to string.

strip.c

Strips the debug information from an executable.

strlen.c

Length of a string.

strncat.c

Adding a fixed number of chars to a string.

strpbrk.c

Finding a character in a string.

strrchr.c

Ditto, but the last char.

tab2spc.c

Convert the tabs to spaces. Contributed by David Hoffman.

tmpfile.c

Creating temporary files.

tmpnam.c

Creating a temporary file name.

valist.c

Functions with variable number of arguments.

 

 

The Language Grammar

 

A.1 Lexical Grammar

 

A.1.1 Lexical Elements

token:

keyword

identifier

constant

string-literal

punctuator

Tokens are either keywords, identifiers, constants, strings or other punctuation symbols. They are the basic elements of the language.

preprocessing-token:

header-name

identifier

pp-number

character-constant

string-literal

punctuator

A pre-processing token is the result of a pre-processor action. It can be either a header name to <include>, identifiers, etc.

A.1.2 Keywords[3]

 

keyword: one of

 

auto

break

case

char

Const

continue

default

do

Double

else

enum

extern

Float

for

goto

if

Inline

int

long

register

(restrict)

return

short

signed

Sizeof

static

struct

switch

Typedef

union

unsigned

void

Volatile

while

_Bool

(_Complex)

(_Imaginary)

operator[4]

 

 

 

A.1.3 Identifiers

identifier:

identifier-nondigit

identifier identifier-nondigit

identifier digit

An identifier must start with a non-digit character, followed by either identifiers or digits. The maximum identifier length is 255 characters in lcc-win32.

 

identifier-nondigit:

nondigit

universal-character-name

nondigit: one of

_a b c d e f g h i j k l m n o p q r s t u v w x y z

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

All characters from 127 to 254[5]

digit: one of

0 1 2 3 4 5 6 7 8 9

A.1.4 Universal character names

universal-character-name:

\u hex-quad

\U hex-quad hex-quad

hex-quad:

hexadecimal-digit hexadecimal-digit

hexadecimal-digit hexadecimal-digit

A.1.5 Constants

constant:

integer-constant

floating-constant

enumeration-constant

character-constant

Constants are either integers, floats, enums or character constants.

integer-constant:

decimal-constant integer-suffix opt

octal-constant integer-suffix opt

hexadecimal-constant integer-suffix (optional)

decimal-constant:

nonzero-digit

decimal-constant digit

octal-constant:

0 octal-constant octal-digit

hexadecimal-constant:

hexadecimal-prefix hexadecimal-digit

hexadecimal-constant hexadecimal-digit

hexadecimal-prefix: one of

0x 0X

nonzero-digit: one of

1 2 3 4 5 6 7 8 9

octal-digit: one of

0 1 2 3 4 5 6 7

hexadecimal-digit: one of

0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

integer-suffix:

unsigned-suffix long-suffix opt

unsigned-suffix long-long-suffix

long-suffix unsigned-suffix opt

long-long-suffix unsigned-suffix opt

unsigned-suffix: one of

u U

long-suffix: one of

l L

long-long-suffix: one of

ll LL

floating-constant:

decimal-floating-constant

hexadecimal-floating-constant[6]

decimal-floating-constant:

fractional-constant exponent-part opt floating-suffix opt

digit-sequence exponent-part floating-suffix opt

hexadecimal-floating-constant:[7]

hexadecimal-prefix hexadecimal-fractional-constant

binary-exponent-part floating-suffix opt

hexadecimal-prefix hexadecimal-digit-sequence

binary-exponent-part floating-suffix opt

fractional-constant:

digit-sequence opt . digit-sequence

digit-sequence .

exponent-part:

e sign opt digit-sequence

E sign opt digit-sequence

sign: one of

+-

digit-sequence:

digit

digit-sequence digit

hexadecimal-fractional-constant:

hexadecimal-digit-sequence opt .

hexadecimal-digit-sequence

hexadecimal-digit-sequence .

binary-exponent-part:

p sign opt digit-sequence

P sign opt digit-sequence

hexadecimal-digit-sequence:

hexadecimal-digit

hexadecimal-digit-sequence hexadecimal-digit

floating-suffix: one of

f l F L

enumeration-constant:

identifier

character-constant:

c-char-sequence

L’c-char-sequence

c-char-sequence:

c-char

c-char-sequence c-char

 c-char:

any member of the source character set except

the single-quote , backslash \, or new-line character

escape-sequence

escape-sequence:

simple-escape-sequence

octal-escape-sequence

hexadecimal-escape-sequence

universal-character-name

simple-escape-sequence: one of

\’ \" \? \\ \a \b \f \n \r \t \v

octal-escape-sequence:

\ octal-digit

\ octal-digit octal-digit

\ octal-digit octal-digit octal-digit

hexadecimal-escape-sequence:

\x hexadecimal-digit

hexadecimal-escape-sequence hexadecimal-digit

A.1.6 String literals

string-literal:

"s-char-sequence opt "         Characters encoded in 1 byte

L"s-char-sequence opt "        Characters encoded in 2 bytes

s-char-sequence:

s-char

s-char-sequence s-char

s-char:

any member of the source character set except the double-quote ", backslash \, or new-line character

escape-sequence

 

A.1.7 Punctuators

punctuator: one of

[](){}.-> ++ -- & * + - ˜ ! /%<<>><><=>===!=ˆ|&&||

?:;... = *= /= %= += -= <<= >>= &= ˆ= |= ,###

<: :> <% %> %: %:%:

 

A.1.8 Header Names

header-name:

<h-char-sequence>            System includes, normally in \lcc\include

"q-char-sequence"            User includes, normally in current directory

h-char-sequence:

h-char

h-char-sequence h-char

h-char:

any member of the source character set except

the new-line character and >       Note that lcc-win32 will translate ‘/’ into ‘\’

q-char-sequence:

q-char

q-char-sequence q-char

q-char:

any member of the source character set except the new-line character and "

A.1.9 Preprocessing Numbers

pp-number:

digit

. digit

pp-number digit

pp-number identifier-nondigit

pp-number e sign

pp-number E sign

pp-number p sign

pp-number P sign

pp-number .

A.2 Phrase Structure Grammar

 

A.2.1 Expressions

primary-expression:

identifier

constant

string-literal

( expression )

postfix-expression:

primary-expression

postfix-expression [ expression ]

            (indexing)

postfix-expression ( argument-expression-list opt )

            (function call)

postfix-expression . identifier

(Structure dereferencing)

postfix-expression -> identifier

            (Structure pointer dereferencing)

postfix-expression ++

     (Increment by one returning the old value)

postfix-expression

     (Decrement by one returning the old value)

( type-name ){initializer-list }[8]

( type-name ){initializer-list ,}

argument-expression-list:

assignment-expression

argument-expression-list , assignment-expression

unary-expression:

postfix-expression

++ unary-expression

     (Increment by one returning the new value)

-- unary-expression

     (Decrement by one returning the new value)

unary-operator cast-expression

sizeof unary-expression

                                                            Returns size of object in bytes

sizeof ( type-name )

unary-operator: one of

&*+-˜!

cast-expression:

unary-expression

( type-name ) cast-expression

multiplicative-expression:

cast-expression

multiplicative-expression * cast-expression

multiplicative-expression / cast-expression

multiplicative-expression % cast-expression             Mod operator: 5%3=2

additive-expression:

multiplicative-expression

additive-expression + multiplicative-expression

additive-expression - multiplicative-expression

shift-expression:

additive-expression

shift-expression << additive-expression                   Left shift

shift-expression >> additive-expression                   Right shift

relational-expression:

shift-expression

relational-expression < shift-expression

relational-expression > shift-expression

relational-expression <= shift-expression

relational-expression >= shift-expression

equality-expression:

relational-expression

equality-expression ==relational-expression

equality-expression != relational-expression

AND-expression:

equality-expression

AND-expression & equality-expression

exclusive-OR-expression:

AND-expression

exclusive-OR-expression ˆ AND-expression

inclusive-OR-expression:

exclusive-OR-expression

inclusive-OR-expression | exclusive-OR-expression

logical-AND-expression:

inclusive-OR-expression

logical-AND-expression && inclusive-OR-expression

logical-OR-expression:

logical-AND-expression

logical-OR-expression || logical-AND-expression

conditional-expression:

logical-OR-expression

logical-OR-expression ? expression : conditional-expression

assignment-expression:

conditional-expression

unary-expression assignment-operator assignment-expression

assignment-operator: one of

= *= /= %= += -= <<= >>= &= ˆ= |=

expression:

assignment-expression

expression , assignment-expression

constant-expression:

conditional-expression

 

A.2.2 Declarations

 

The purpose of declarations is to define the type of an object to the compiler.

 

declaration:

declaration-specifiers init-declarator-list opt ;

declaration-specifiers:

storage-class-specifier declaration-specifiers opt

type-specifier declaration-specifiers opt

type-qualifier declaration-specifiers opt

function-specifier declaration-specifiers opt

init-declarator-list:

init-declarator

init-declarator-list , init-declarator

init-declarator:

declarator

declarator = initializer

storage-class-specifier:

typedef            No storage, just a compile time definition.

extern             Storage is defined in another module.

static             Storage is allocated in this module, but

not visible outside.

auto               Stack storage in current frame.

register                            Integer variable may be stored in a machine register.

type-specifier:

void

char

short

int

long

float

double

signed

unsigned

_Bool

_Complex

_Imaginary

struct-or-union-specifier

enum-specifier

typedef-name

struct-or-union-specifier:

struct-or-union identifier opt { struct-declaration-list }

struct-or-union identifier

struct-or-union:

struct             Each field is consecutive storage.

union              All fields occupy the same space as the largest one.

struct-declaration-list:

struct-declaration

struct-declaration-list struct-declaration

struct-declaration:

specifier-qualifier-list struct-declarator-list ;

specifier-qualifier-list:

type-specifier specifier-qualifier-list opt

type-qualifier specifier-qualifier-list opt

struct-declarator-list:

struct-declarator

struct-declarator-list , struct-declarator

struct-declarator:

declarator

declarator opt : constant-expression

enum-specifier:

enum identifier opt { enumerator-list }

enum identifier opt { enumerator-list ,}

enum identifier

enumerator-list:

enumerator

enumerator-list , enumerator

enumerator:

enumeration-constant

enumeration-constant = constant-expression

type-qualifier:

const

restrict

volatile

 function-specifier:

inline

_stdcall                [9]

_ _declspec(dllexport)

overloaded[10]

declarator:

pointer opt direct-declarator

reference opt direct-declarator[11]

direct-declarator:

identifier

( declarator )

direct-declarator [ assignment-expression opt ]

direct-declarator [*]

direct-declarator ( parameter-type-list )

direct-declarator ( identifier-list opt )

identifier . identifier

pointer:

* type-qualifier-list opt

* type-qualifier-list opt pointer

reference:

type-qualifier-list &                             lcc-win32 extension.

type-qualifier-list:

type-qualifier

type-qualifier-list type-qualifier

parameter-type-list:

parameter-list

parameter-list , ...

parameter-list:

parameter-declaration

parameter-list , parameter-declaration

parameter-declaration:

declaration-specifiers declarator

declaration-specifiers abstract-declarator opt

identifier-list:

identifier

identifier-list , identifier

type-name:

specifier-qualifier-list abstract-declarator opt

abstract-declarator:

pointer

pointer opt direct-abstract-declarator

direct-abstract-declarator:

( abstract-declarator )

direct-abstract-declarator opt [ assignment-expression opt ]

direct-abstract-declarator [*]

direct-abstract-declarator opt ( parameter-type-list opt )

typedef-name:

identifier

initializer:

assignment-expression

{ initializer-list }

{ initializer-list ,}

initializer-list:

designation opt initializer

initializer-list , designation opt initializer

designation:

designator-list =

designator-list:

designator

designator-list designator

designator:

[ constant-expression ]

. identifier

A.2.3 Statements

statement:

labeled-statement

compound-statement

expression-statement

selection-statement

iteration-statement

jump-statement

labeled-statement:

identifier : statement

case constant-expression : statement

default : statement

compound-statement:

{ block-item-list opt }

block-item-list:

block-item

block-item-list block-item

block-item:

declaration

statement

expression-statement:

expression opt ;

selection-statement:

if ( expression ) statement

if ( expression ) statement else statement

switch ( expression ) statement

iteration-statement:

while ( expression ) statement

do statement while ( expression );

for ( expression opt ; expression opt ; expression opt ) statement

for ( declaration expression opt ; expression opt ) statement

jump-statement:

goto identifier ;

continue ;

break ;

return expression opt ;

A.2.4 External Definitions

translation-unit:

external-declaration

translation-unit external-declaration

external-declaration:

function-definition

declaration

function-definition:

declaration-specifiers declarator declaration-list opt compound-statement

operator operator-name declaration-list compound-statement[12]

declaration-specifiers overloaded declarator declaration-list compound statement

declaration-list:

declaration

declaration-list declaration

operator-name:

     + - * / << += -= *= /= & | ^ >> < <= >= > ~ ! 

A.3 Preprocessing Directives

preprocessing-file:

group opt

group:

group-part

group group-part

group-part:

pp-tokens opt new-line

if-section

control-line

if-section:

if-group elif-groups opt else-group opt endif-line

if-group:

#if constant-expression new-line group opt

# ifdef identifier new-line group opt

# ifndef identifier new-line group opt

elif-groups:

elif-group

elif-groups elif-group

elif-group:

# elif constant-expression new-line group opt

else-group:

# else new-line group opt

(6.10.1) endif-line:

# endif new-line

control-line:

# include pp-tokens new-line

# define identifier replacement-list new-line

# define identifier lparen identifier-list opt )

replacement-list new-line

# define identifier lparen ... ) replacement-list new-line

# define identifier lparen identifier-list , ... )

replacement-list new-line

# undef identifier new-line

# line pp-tokens new-line

# error pp-tokens opt new-line

# pragma pp-tokens opt new-line

# new-line

lparen:

the left-parenthesis character without preceding white space[13]

replacement-list:

pp-tokens opt

pp-tokens:

preprocessing-token

pp-tokens preprocessing-token

new-line:

the new-line character

Appendix 3: Implementation Limits

 

Limit

Effect

More than 4095 characters in a string literal

Warning message

More than 127 levels of nested statements

Warning message

More than 1023 cases in a switch statement

Warning message

More than 511 identifiers declared in a block

Warning message

More than 65535 bytes in an object

Warning message

More than 2147483647 bytes in an array

Error

More than 1023 fields in a struct/union

Warning message

More than 1023 enumeration members

Warning message

More than 127 arguments to a function

Warning message

More than 4095 external identifiers

Warning message

More than 64 #if nesting levels

Error

More than 128 evaluation levels in #if directive

Error

More than 32 #include nesting levels

Error

More than 256 arguments to a macro

Error

More than 20 dimensions in a variable length array

Error

 

Bibliography

 

The following books may be of interest for learning how to program under Windows:

Programming Windows, Petzold (and Yao), MS Press

Charles Petzold wrote the original Windows programming 'bible' - "Programming Windows" back in the mid eighties. Everyone who started Windows programming in those days had (or needed) a copy of this book. Now updated for Win32, Petzold starts the basic paradigms of Windows programming, Windows and message queues. He goes on to cover many advanced topics. The book uses C and assumes a prior knowledge of this language.  I have tried to get all of the examples of this book to work. If you find any problems please report them to me!

Windows 95 System Programming Secrets by Matt Pietrek

A good book to investigate. All low level details of the Win32 API. It complements the explanations in the lcc-win32 technical reference very well.

 

These magazines are good references and can be useful for your work:

 

Microsoft Systems Journal

A monthly magazine containing the most up-to-date official view of Windows development. Contrary to what anyone would expect, sometimes you can find highly critical articles about Microsoft products!

 

Windows DOS Developer’s Journal

This is a must for Windows developers. Highly technical contents, very good book reviews.

 

Dr. Dobb’s Journal

Another regular magazine. For those who want to know what is happening beyond the Windows horizon. There are articles about Windows, but they are not the only content of this magazine.

 



[1] This is distributed in the CDROM and e-mail versions only.

[2] You can subscribe to the Microsoft documentation MSDN. Look at http://msdn.microsoft.com for details.

[3] Keywords in parentheses are NOT implemented in lcc-win32

[4] The operator identifier is not a keyword strictu sensu, since its meaning depends on the context where it is defined. See operator overloading.

[5] Note that this is not strictly specified by the standard of the language and the programs accepted by lcc-win32 may not compile in other processors.

[6] This is not done yet.

[7] This is not implemented in lcc-win32

[8] The implementation of these two features has started, but will not be ready until July/August 2000.

[9] This specifiers are specific to the Windows system, and are not supported by other compilers in other systems. Within the Windows system they are fairly common.

[10] See the documentation for the overloaded keyword .

[11] References, even if widely used in the C++ language are not strictly part of the C grammar.

[12] Note that this is specific to lcc-win32, even if this type of operator redefinition is widely used in C++, it is not a part of the C language.

[13] Note that here is one of the few places where white space is significant in the language.